package cn.rocky.prepare;

import java.util.HashMap;
import java.util.Map;

/**
 * 同构字符串
 */
public class isIsomorphic {

    public static boolean isBlank(String args) {
        return args == null || args == "";
    }

    public static boolean isIsomorphic(String arg1, String arg2) {
        Map<Character,Character> map1 = new HashMap<>();
        Map<Character,Character> map2 = new HashMap<>();
        if (isBlank(arg1) || isBlank(arg2)) {return false;} //为空则默认不是同构字符串
        if (arg1.length() != arg2.length()) {return false;}
        int length = arg1.length();
        for (int i = 0; i < length; i++) {
            char arg1Value = arg1.charAt(i), arg2Value = arg2.charAt(i);
            // 当map里面包含该key 时检查该key对应的值是否与当前字符串对应位置的值是否相等
            boolean map1Flag = map1.containsKey(arg1Value) && map1.get(arg1Value) != arg2Value;
            boolean map2Flag = map2.containsKey(arg2Value) && map2.get(arg2Value) != arg1Value;
            if (map1Flag || map2Flag) {return false;}
            map1.put(arg1Value, arg2Value);
            map2.put(arg2Value, arg1Value);
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isIsomorphic("abcdd", "qwerr"));
    }
}
